/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.itschapala.com.dsw2.vista;

import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import mx.itschapala.com.dsw2.Bl.ArticuloBlLocal;
import mx.itschapala.com.dsw2.modelo.Articulo;

/**
 *
 * @author z
 */
@ManagedBean
@SessionScoped
public class ArticuloBean {
    
    
     //----- EJBS---//
    @EJB
    private ArticuloBlLocal articuloBl;

   //------------variables---------------- 
   private List<Articulo> lista;
  private Articulo articulo;
  private String accion;
    public ArticuloBean() {
     articulo = new  Articulo();
    }
//--------------get y set--------------

    public List<Articulo> getLista() {
        return lista;
    }

//    public void setLista(List<Articulo> lista) {
//        this.lista = lista;
//    }

    public Articulo getArticulo() {
        return articulo;
    }

    public void setArticulo(Articulo articulo) {
        this.articulo = articulo;
    }
    
   //-------------acciones-----------------

    
    public String relizarAccion(){
    if(accion.equalsIgnoreCase("nuevo")){
    articuloBl.register(articulo);
    }else{
    if(accion.equalsIgnoreCase("Editar")){
    articuloBl.modify(articulo);
    }else{
    if(accion.equalsIgnoreCase("Eliminar")){
  articuloBl.delete(articulo);
        }
    }
    }
    accion="";
    return "AdministradorLista";

}    
 
//-------------listeners----------------
    
    
public void preparaNuevo(ActionEvent e){
    articulo=new Articulo(); 
    accion="Nuevo";
}
public void preparaEditar(ActionEvent evt){
       System.out.println(evt.getComponent().getClientId());
       int index = Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
       System.out.println("el numero es:"+ index);
      articulo=lista.get(index);
       accion="Editar";               
}
public void preparaEliminar(ActionEvent ev){
    int index = Integer.parseInt(ev.getComponent().getClientId().split(":")[2]);      
       articulo=lista.get(index);
accion="Eliminar";
}
   
}

 

 
    
    
    

